<template>
  <!-- 规则定义子页面 输出内容-->
  <div>
    <el-form
      :model="addRuleExport"
      ref="addRuleExport"
      label-position="right"
      :rules="addRuleExportRules"
    >
      <el-row>
        <el-col :span="24">
          <h3 style="line-height: 40px; font-weight: bolder; font-size: 13px">
            操作目标
          </h3>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="输出结果:"
            prop="pubPhysicaltableId"
            label-width="80px"
          >
            <el-select
              v-model="addRuleExport.pubPhysicaltableId"
              placeholder="请选择输出结果"
              style="width: 100%"
              @change="tableNameEnChanges"
              :disabled="pubDisabled || operateType == '3'"
            >
              <el-option
                v-for="item in pubPhysicaltableList"
                :key="item.tableNameEn"
                :label="item.tableNameCn + '--' + item.tableNameEn"
                :value="item.tableNameEn"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="结果值:"
            prop="pubPhysicalcolumnId"
            label-width="90px"
          >
            <el-select
              v-model="addRuleExport.pubPhysicalcolumnId"
              placeholder="请选择结果值"
              style="width: 100%"
              :disabled="pubDisabled || operateType == '3'"
            >
              <el-option
                v-for="item in columnList"
                :key="item.columnEn"
                :label="item.columnCn + '--' + item.columnEn"
                :value="item.columnEn"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" v-if="feeFieldShow">
          <el-form-item
            label="费用接收字段:"
            prop="feeField"
            label-width="110px"
          >
            <el-select
              :disabled="operateType == '3'"
              v-model="addRuleExport.feeField"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in feeFieldList"
                :key="item.fieldCode"
                :label="item.fieldName"
                :value="item.fieldCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <h3 style="line-height: 40px; font-weight: bolder; font-size: 13px">
            筛选条件
          </h3>
          <!-- 筛选条件部分打算用组件 -->
        </el-col>
      </el-row>
      <el-row style="font-size: 13px; margin-bottom: 10px">
        <el-col :span="7">
          <p>维度</p>
        </el-col>
        <el-col :span="11">
          <p>值</p>
        </el-col>
        <el-col :span="6">
          <p>描述</p>
        </el-col>
      </el-row>
      <div
        style="
          height: 180px;
          overflow: auto;
          overflow-x: hidden;
          margin-bottom: 20px;
        "
      >
        <el-row
          v-for="(item, index) in selectConditionList"
          :key="index"
          style="margin-top: 10px"
        >
          <el-col :span="7">
            <span style="line-height: 25px">{{ item.columnCn }}</span>
          </el-col>
          <el-col :span="5">
            <el-select
              :disabled="operateType == '3'"
              v-model="item.filterValue"
              placeholder="请选择"
              style="width: 100%"
              @change="updataMethods($event,index)"
            >
              <el-option
                v-for="item in selectConditionList[index].filterValueList"
                :key="item.filterValue"
                :label="item.filterName"
                :value="item.filterValue"
              >
              </el-option>
            </el-select>
            <!-- <el-input readonly v-model="item.filterValue"></el-input> -->
          </el-col>
          <el-col :span="6">
            <el-button
              :disabled="operateType == '3'"
              style="min-width: 60px; min-height: 26px"
              @click.native="toSelectTab(item, index)"
              >选择</el-button
            >
          </el-col>
          <el-col :span="6">
           <span v-if="item.selectionFlag != '1'" style="line-height: 25px">{{ item.filterValue }}</span>
           <span v-if="item.selectionFlag == '1'" style="line-height: 25px">{{ item.filterValue.split('@@@')[0] }}</span>
            <!-- <span style="line-height: 25px">{{ item.filterValue }}</span> -->
          </el-col>
        </el-row>
      </div>
      <el-col :span="24">
        <h3
          style="line-height: 40px; font-weight: bolder; font-size: 13px"
        ></h3>
        <!-- 筛选条件部分打算用组件 -->
      </el-col>
    </el-form>
    <!-- 所有成员、关系树展示-->
    <div v-if="toShowMemberTree">
      <memberAndTree
        ref="memberAndTree"
        :toShowMemberTree="toShowMemberTree"
        :indexItems="indexItems"
        :dictCode="dictCode"
        :itemData="itemData"
        @selectDataFunc="selectDataFunc"
      ></memberAndTree>
    </div>
    <!-- 所有成员、关系树展示-->
  </div>
</template>
<script>
import { ruleDefinition } from "@/api/index";
// 基本信息
import basicMsg from "@/components/ca/basicMsg.vue";
// 所有成员、关系树
import memberAndTree from "@/components/ca/memberAndTree";

export default {
  name: "exportData",

  props: {
    ruleCode: {
      type: String,
      default: "",
    },
    operateType: {
      type: String,
      default: "1",
    },
  },
  data() {
    return {
      ruleType: "0",
      feeFieldShow: false,
      selectData: [], // 获取动态回显数据
      feeFieldList: [], // 费用接收字段
      pubDisabled: false,
      dictCode: "",
      indexItems: 0,
      itemData: [],
      columnList: [],
      pubPhysicaltableList: [],
      toShowMemberTree: false,
      selectConditionList: [],
      addRuleExport: {
        pubPhysicaltableId: "",
        pubPhysicalcolumnId: "",
        debitDatafilterId: "",
        feeField: "",
      },
      addRuleExportRules: {
        pubPhysicaltableId: [
          { required: true, message: "请选择输出结果", trigger: "blur" },
        ],
        pubPhysicalcolumnId: [
          { required: true, message: "请选择结果值", trigger: "blur" },
        ],
        feeField: [
          { required: true, message: "请选择费用接收字段", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    basicMsg,
    memberAndTree,
  },
  created() {
    this.ruleTypeChangeInit();
    this.ruleTypeChange();
    this.addRuleSource();
  },
  mounted() {
  },
  watch: {
  },
  methods: {
    // 获取从基础信息传递过来的规则类型
    ruleTypeChangeInit() {
      var _self = this;
      this.bus.$on("ruleTypeChangeInit", function (val) {
        // 机构层级
        if (val.ruleType == "0") {
          _self.ruleType = "0";
          console.log(_self.ruleType, "238");
          _self.getPubPhysicaltableData();
          _self.pubDisabled = true;
          _self.feeFieldShow = false;
        }
        // 账户层级
        if (val.ruleType == "1") {
          _self.ruleType = "1";
          console.log(_self.ruleType, "246");
          _self.getPubPhysicaltableData();
          _self.pubDisabled = false;
          _self.feeFieldShow = true;
        }
      });
    },
    ruleTypeChange() {
      var _self = this;
      this.bus.$on("ruleTypeChange", function (val) {
        // 机构层级
        if (val.ruleType == "0") {
          _self.ruleType = "0";
          console.log(_self.ruleType, "238");
          _self.getPubPhysicaltableData();
          _self.pubDisabled = true;
          _self.feeFieldShow = false;
          (_self.addRuleExport.pubPhysicaltableId = ""),
          (_self.addRuleExport.pubPhysicalcolumnId = "");
        }
        // 账户层级
        if (val.ruleType == "1") {
          _self.ruleType = "1";
          console.log(_self.ruleType, "246");
          _self.getPubPhysicaltableData();
          _self.pubDisabled = false;
          _self.feeFieldShow = true;
          (_self.addRuleExport.pubPhysicaltableId = ""),
          (_self.addRuleExport.pubPhysicalcolumnId = "");
        }
      });
    },
    addRuleSource() {
      let _self = this;
      this.bus.$on("addRuleSource", function (val) {
        if (_self.ruleType == "0") {
          _self.addRuleExport.pubPhysicaltableId = val.tableNameEn;
          if (_self.addRuleExport.pubPhysicaltableId) {
            _self.tableNameEnChange();
          }
          (_self.addRuleExport.pubPhysicalcolumnId = val.columnEn),
            this.$forceUpdate();
        }
      });
    },
    updataMethods(val,index) {
      this.selectConditionList[index].filterName = this.selectConditionList[index].filterValueList.find(
                item => item.filterValue == val
            ).filterName
    },
    // 清除数据
    clearMsg() {
      this.toShowMemberTree = false;
      this.$nextTick(() => {
        this.$refs.addRuleExport.clearValidate();
      });
      this.selectConditionList = [];
      for (let i in this.addRuleExport) {
        this.addRuleExport[i] = "";
      }
      this.pubDisabled = false;
      this.feeFieldShow = false;
      this.init();
    },
    // 获取维度成员
    selectDataFunc(val) {
      this.toShowMemberTree = false;
      if (val) {
        let params = val;
        if (val.selectionFlag == "2") {
          let ind = params.index;
          let val = {
            filterName: params.row.dictItemName,
            filterValue: params.row.dictItemCode,
          };
          this.selectConditionList[ind].filterValueList = [
            {
              filterValue: "-98",
              filterName: "同数据来源",
            },
            {
              filterValue: "-97",
              filterName: "同分摊动因",
            },
            {
              filterValue: "-99",
              filterName: "同数据来源和同分摊动因",
            },
          ];
          this.selectConditionList[ind].filterValueList.push(val);
          this.selectConditionList[ind].filterValue = params.row.dictItemCode;
          this.selectConditionList[ind].filterName = params.row.dictItemName;
          this.selectConditionList[ind].selectionFlag = "2";
          this.$forceUpdate();
        }
        if (val.selectionFlag == "1") {
          let ind = params.index;
          let val = {
            filterValue: params.row.id,
            filterName: params.row.name,
          };
          this.selectConditionList[ind].filterValueList = [
            {
              filterValue: "-98",
              filterName: "同数据来源",
            },
            {
              filterValue: "-97",
              filterName: "同分摊动因",
            },
            {
              filterValue: "-99",
              filterName: "同数据来源和同分摊动因",
            },
          ];
          this.selectConditionList[ind].filterValueList.push(val);
          this.selectConditionList[ind].filterValue = params.row.id;
          this.selectConditionList[ind].filterName = params.row.name;
          this.selectConditionList[ind].treeId = params.row.treeCode;
          this.selectConditionList[ind].selectionFlag = "1";
        }
      }
    },
    // 获取结果值
    getColumn() {
      let params = {
        isShow: "1",
        isTemplate: "0",
        tableNameEn: this.addRuleExport.pubPhysicaltableId,
      };
      ruleDefinition
        .queryListCa(params)
        .then((res) => {
          if (res.code == 200) {
            this.columnList = res.datas;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    init() {
      this.getPubPhysicaltableData();
      this.getFeeFieldList();
      if (
        this.operateType == "2" ||
        this.operateType == "3" ||
        this.operateType == "5"
      ) {
        this.getRuleExportDataToForm();
      }
    },
    getPubPhysicaltableData() {
      console.log(this.ruleType, "347");
      let params = {
        ruleType: this.ruleType,
      };
      ruleDefinition
        .qeuryListForRule(params)
        .then((res) => {
          if (res.code == 200) {
            this.pubPhysicaltableList = res.datas;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((res) => {
          console.log(res);
        });
    },
    // 获取费用接收字段
    getFeeFieldList() {
      ruleDefinition
        .pubCountFeeFieldQueryList()
        .then((res) => {
          if (res.code == 200) {
            this.feeFieldList = res.datas;
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((res) => {
          console.log(res);
        });
    },
    toSelectTab(item, index) {
      var _self = this;
      this.toShowMemberTree = true;
      this.indexItems = index;
      this.itemData = item.filterValueList.map((v) => {
        return v.filterValue;
      });
      this.itemData = [item.filterValue];
      this.dictCode = item.relatedDimensionCode;
    },

    //编辑详情 获取回显数据
    getRuleExportDataToForm() {
      let params = {
        ruleCode: this.ruleCode,
        elementType: "3", // 输出内容写死
      };
      ruleDefinition
        .ruleBaseDetailToOutput(params)
        .then((res) => {
          if (res.code == 200) {
            this.addRuleExport.pubPhysicaltableId =
              res.datas.debit.pubPhysicaltableId; // 输出结果
            this.addRuleExport.pubPhysicalcolumnId =
              res.datas.debit.pubPhysicalcolumnId; // 结果值
            this.addRuleExport.feeField = res.datas.debit.fieldCode; // 费用字段标示
            this.addRuleExport.debitDatafilterId = res.datas.debit.datafilterId; // 标示
            // 获取筛选条件
            if (this.addRuleExport.pubPhysicaltableId) {
              this.tableNameEnChange();
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    tableNameEnChange() {
      // 获取字段
      this.getColumn();
      this.getFilterOptions();
    },
    tableNameEnChanges() {
      // 获取字段
      this.addRuleExport.pubPhysicalcolumnId = "";
      this.getColumn();
      this.getFilterOptions();
    },
    getFilterOptions() {
      let params = {
        // relatedDimensionCode: this.ruleCode,
        isShow: "1",
        isTemplate: "0",
        tableNameEn: this.addRuleExport.pubPhysicaltableId,
      };
      ruleDefinition
        .queryDimColumn(params)
        .then((res) => {
          if (res.code == 200) {
            this.selectConditionList = res.datas;
            if (this.selectConditionList.length >= 1) {
              for (let i = 0; i < this.selectConditionList.length; i++) {
                this.$set(this.selectConditionList[i], "filterValue", "-98");
                this.$set(this.selectConditionList[i], "treeId", "");
                this.$set(this.selectConditionList[i], "selectionFlag", "-");
                this.$set(
                  this.selectConditionList[i],
                  "filterName",
                  "同数据来源"
                );
                this.selectConditionList[i].selectionFlag = "-";
                this.selectConditionList[i].treeId = "";

                this.selectConditionList[i].filterValueList = [
                  {
                    filterValue: "-98",
                    filterName: "同数据来源",
                  },
                  {
                    filterValue: "-97",
                    filterName: "同分摊动因",
                  },
                  {
                    filterValue: "-99",
                    filterName: "同数据来源和同分摊动因",
                  },
                ];
                this.selectConditionList[i].filterValue = "-98";
              }
              if (this.operateType && this.operateType != "1") {
                // 编辑查看复制获取回显的值
                this.getRuleBaseDetailGetDataFilter();
              }
            }
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 编辑查看复制 获取筛选条件动态选择框信息
    getRuleBaseDetailGetDataFilter() {
      let params = {
        datafilterId: this.addRuleExport.debitDatafilterId,
      };
      ruleDefinition
        .ruleBaseDetailGetDataFilter(params)
        .then((res) => {
          if (res.code === 200) {
            this.selectData = res.datas;
            if (this.selectData.length) {
              for (let i = 0; i < this.selectData.length; i++) {
                for (let t = 0; t < this.selectConditionList.length; t++) {
                  console.log(
                    this.selectConditionList[t].columnEn,
                    this.selectData[i].dimValue,
                    "hahhah507"
                  );
                  if (
                    this.selectConditionList[t].columnEn ==
                    this.selectData[i].dimValue
                  ) {
                    // 树结构加@@@
                    if (this.selectData[i].selectionFlag == "1") {
                      this.selectConditionList[t].filterValue =
                        this.selectData[i].filterValue +
                        "@@@" +
                        this.selectData[i].treeId;
                    } else {
                      this.selectConditionList[t].filterValue = this.selectData[
                        i
                      ].filterValue;
                    }
                    this.selectConditionList[t].filterName = this.selectData[
                      i
                    ].filterName;
                    this.selectConditionList[t].selectionFlag = this.selectData[
                      i
                    ].selectionFlag;
                    this.selectConditionList[t].treeId = this.selectData[
                      i
                    ].treeId;
                    //
                    this.selectConditionList[t].filterValueList = [
                      {
                        filterValue: "-98",
                        filterName: "同数据来源",
                      },
                      {
                        filterValue: "-97",
                        filterName: "同分摊动因",
                      },
                      {
                        filterValue: "-99",
                        filterName: "同数据来源和同分摊动因",
                      },
                    ];
                    if (
                      this.selectConditionList[t].filterValue != "-98" &&
                      this.selectConditionList[t].filterValue != "-97" &&
                      this.selectConditionList[t].filterValue != "-99"
                    ) {
                      let val = {
                        filterName: this.selectConditionList[t].filterName,
                        filterValue: this.selectConditionList[t].filterValue,
                      };

                      this.selectConditionList[t].filterValueList.push(val);
                    }

                    //
                  }
                }
              }
            }
            console.log(this.selectConditionList, "ppppp");
          } else {
            this.$message.error(res.message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>
<style lang="scss" scoped></style>
